package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.task.ModifyPasswordTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.view.view.ModifyPasswordView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/1/17.
 */

public class ModifyPasswordPresenter implements Presenter {

    private UseCase modifyPassword;
    private AppCompatActivity activity;
    private ModifyPasswordView modifyPasswordView;


    @Inject
    public ModifyPasswordPresenter(UseCase modifyPassword, AppCompatActivity activity) {
        this.modifyPassword = modifyPassword;
        this.activity = activity;
    }

    public void setView(ModifyPasswordView view) {
        this.modifyPasswordView = view;
    }

    public void modifyPassword(String account, String originalPassword, String newPassword) {
        modifyPassword.execute(new ModifyPasswordSubscriber(activity), new ModifyPasswordTask(account, originalPassword, newPassword));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        modifyPassword.unSubscribe();
    }

    private class ModifyPasswordSubscriber extends BaseSubscriber<Boolean> {

        public ModifyPasswordSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean aBoolean) {
            super.onNext(aBoolean);
            modifyPasswordView.modifyPasswordSuccess();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.modify_password_fail);
        }
    }
}
